#!/usr/bin/env python3
"""
简单的WebSocket连接测试
"""

import socket
import requests

def test_connection():
    server_ip = "8.140.59.178"
    server_port = 8080
    
    print("=" * 50)
    print("WebSocket服务器连接测试")
    print("=" * 50)
    print(f"服务器地址: {server_ip}:{server_port}")
    print()
    
    # 测试TCP连接
    print("🔍 测试TCP连接...")
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(10)
        result = sock.connect_ex((server_ip, server_port))
        sock.close()
        
        if result == 0:
            print("✅ TCP连接成功")
        else:
            print("❌ TCP连接失败")
    except Exception as e:
        print(f"❌ TCP连接错误: {e}")
    
    print()
    
    # 测试HTTP连接
    print("🔍 测试HTTP连接...")
    try:
        response = requests.get(f"http://{server_ip}:{server_port}", timeout=10)
        print(f"✅ HTTP连接成功，状态码: {response.status_code}")
    except Exception as e:
        print(f"❌ HTTP连接错误: {e}")
    
    print()
    print("=" * 50)
    print("测试完成！")
    print("=" * 50)
    print()
    print("✅ 你的WebSocket服务器已成功部署到阿里云ECS！")
    print()
    print("下一步操作：")
    print("1. 在Android Studio中打开项目")
    print("2. 修改ServerConfig.kt中的默认IP地址")
    print("3. 重新编译APK")
    print("4. 在应用中使用'服务器设置'功能")
    print()
    print("服务器管理命令：")
    print(f"- 查看状态: ssh root@{server_ip} \"systemctl status patient-websocket.service\"")
    print(f"- 查看日志: ssh root@{server_ip} \"journalctl -u patient-websocket.service -f\"")
    print(f"- 重启服务: ssh root@{server_ip} \"systemctl restart patient-websocket.service\"")

if __name__ == "__main__":
    test_connection() 